package com.atguigu.xuexiplus.service.impl;

import com.atguigu.xuexiplus.dto.HomeworkDTO;
import com.atguigu.xuexiplus.dto.HomeworkVO;
import com.atguigu.xuexiplus.entity.Homework;
import com.atguigu.xuexiplus.mapper.HomeworkMapper;
import com.atguigu.xuexiplus.mapper.HomeworkCompletionMapper;
import com.atguigu.xuexiplus.service.HomeworkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 作业服务实现类
 */
@Service
public class HomeworkServiceImpl extends ServiceImpl<HomeworkMapper, Homework> implements HomeworkService {

    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private HomeworkCompletionMapper completionMapper;

    @Override
    public boolean publishHomework(HomeworkDTO homeworkDTO) {
        Homework homework = new Homework();
        BeanUtils.copyProperties(homeworkDTO, homework);
        homework.setStatus(1); // 已发布
        homework.setCreatedAt(LocalDateTime.now());
        homework.setUpdatedAt(LocalDateTime.now());
        homework.setDeleted(0);
        return save(homework);
    }

    @Override
    public List<HomeworkVO> getHomeworksBySubjectId(Long subjectId) {
        return homeworkMapper.getHomeworksBySubjectId(subjectId);
    }

    @Override
    public List<HomeworkVO> getHomeworksBySubjectIdAndStudent(Long subjectId, Long studentId) {
        List<HomeworkVO> homeworks = homeworkMapper.getHomeworksBySubjectIdAndStudent(subjectId, studentId);
        // 设置是否过期
        LocalDateTime now = LocalDateTime.now();
        for (HomeworkVO homework : homeworks) {
            homework.setIsExpired(homework.getDeadline().isBefore(now));
        }
        return homeworks;
    }

    @Override
    public List<HomeworkVO> getTodayHomeworks() {
        return homeworkMapper.getTodayHomeworks();
    }

    @Override
    public Double getTodayCompletionRate(Long studentId) {
        List<HomeworkVO> all = homeworkMapper.getTodayHomeworksByStudent(studentId);
        if (all == null || all.isEmpty()) {
            return 0.0;
        }
        long completed = all.stream().filter(h -> h.getCompletionStatus() != null && h.getCompletionStatus() == 1).count();
        return Math.round((completed * 100.0 / all.size()) * 10.0) / 10.0;
    }

    @Override
    public List<HomeworkVO> getTodayHomeworksByStudent(Long studentId) {
        List<HomeworkVO> homeworks = homeworkMapper.getTodayHomeworksByStudent(studentId);
        // 设置是否过期
        java.time.LocalDateTime now = java.time.LocalDateTime.now();
        for (HomeworkVO homework : homeworks) {
            homework.setIsExpired(homework.getDeadline().isBefore(now));
        }
        return homeworks;
    }

    @Override
    public boolean toggleCompletion(Long homeworkId, Long studentId) {
        com.atguigu.xuexiplus.entity.HomeworkCompletion exist = completionMapper.findOne(homeworkId, studentId);
        if (exist == null) {
            com.atguigu.xuexiplus.entity.HomeworkCompletion add = new com.atguigu.xuexiplus.entity.HomeworkCompletion();
            add.setHomeworkId(homeworkId);
            add.setStudentId(studentId);
            add.setCompletionStatus(1); // 默认标记为完成
            add.setCreatedAt(java.time.LocalDateTime.now());
            add.setUpdatedAt(java.time.LocalDateTime.now());
            return completionMapper.insert(add) > 0;
        } else {
            int next = (exist.getCompletionStatus() != null && exist.getCompletionStatus() == 1) ? 0 : 1;
            return completionMapper.updateStatus(homeworkId, studentId, next) > 0;
        }
    }

    @Override
    public boolean updateHomework(Long id, HomeworkDTO homeworkDTO) {
        Homework homework = getById(id);
        if (homework == null) {
            return false;
        }
        BeanUtils.copyProperties(homeworkDTO, homework);
        homework.setUpdatedAt(LocalDateTime.now());
        return updateById(homework);
    }

    @Override
    public boolean deleteHomework(Long id) {
        return removeById(id);
    }
}
